package xushuo.dao.entity.weixin.user;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.Date;

/**
 * Create by zhangpe0312@qq.com on 2018/2/6.
 * <p>
 * 根据用户登陆，获取到用户的openid和session_key
 * 在程序用由于openid为用户唯一表示，所以以此为主表，关联该用户的
 * 其他信息
 *
 * 全部查找用户信息的，都将通过从APP中获取到的openid进行查询
 * @see GetPhoneNum
 * @see GetUserInfo
 * @see ChooseAddressInfo
 */
@Entity
@Table(name = "UserLoginInfo")
public class UserLoginInfo implements GetInfo {

    private String id;
    private String openid;
    private String session_key;
    private Date getTime;

    //与这个用户相关的一些信息
    private GetUserInfo userInfo;
    private GetPhoneNum phoneNum;
    private ChooseAddressInfo addressInfo;

    @Id
    @Column(name = "id", unique = true, length = 32, nullable = false)
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "uuid")
    public String getId() {
        return id;
    }

    @Column(name = "openid", unique = true, length = 32, nullable = false)
    public String getOpenid() {
        return openid;
    }

    @Column(name = "session_key", length = 32, nullable = false)
    public String getSession_key() {
        return session_key;
    }

    @Column(name = "getTime", length = 19, nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    public Date getGetTime() {
        return getTime;
    }

    @OneToOne
    @JoinColumn(name = "userInfo_id")
    public GetUserInfo getUserInfo() {
        return userInfo;
    }

    @OneToOne
    @JoinColumn(name = "phoneNum_id")
    public GetPhoneNum getPhoneNum() {
        return phoneNum;
    }

    @OneToOne
    @JoinColumn(name = "addressInfo_id")
    public ChooseAddressInfo getAddressInfo() {
        return addressInfo;
    }

    public static class Builder {
        UserLoginInfo userLoginInfo = new UserLoginInfo();

        public Builder setId(String id) {
            userLoginInfo.setId(id);
            return this;
        }

        public Builder setOpenid(String openid) {
            userLoginInfo.setOpenid(openid);
            return this;
        }

        public Builder setSession_key(String session_key) {
            userLoginInfo.setSession_key(session_key);
            return this;
        }

        public Builder setUserInfo(GetUserInfo userInfo) {
            userLoginInfo.setUserInfo(userInfo);
            return this;
        }

        public Builder setPhoneNum(GetPhoneNum phoneNum) {
            userLoginInfo.setPhoneNum(phoneNum);
            return this;
        }

        public Builder setAddressInfo(ChooseAddressInfo addressInfo) {
            userLoginInfo.setAddressInfo(addressInfo);
            return this;
        }

        public Builder setGetTime(){
            userLoginInfo.setGetTime(new Date());
            return this;
        }

        public UserLoginInfo keep(){
            return userLoginInfo;
        }
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public void setSession_key(String session_key) {
        this.session_key = session_key;
    }

    public void setGetTime(Date getTime) {
        this.getTime = getTime;
    }

    public void setUserInfo(GetUserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public void setPhoneNum(GetPhoneNum phoneNum) {
        this.phoneNum = phoneNum;
    }

    public void setAddressInfo(ChooseAddressInfo addressInfo) {
        this.addressInfo = addressInfo;
    }
}
